<?php
/*
 * autor: Juan Manuel Rodriguez
 * */
require_once('constantesMvial.php'); 

$flagPays = false;
$flagPay = false;
$flagPayment = false;
$orderAndMoney=array();
$currentPayOrder=0;

function doPost($url, $post = null)
{
	$context = array();	
	if (is_array($post))
	{
	ksort($post);	
	$context['http'] = array
	(
		'method' => 'GET',
		'content' => http_build_query($post, '', '&'),
	);
	}	
#	return file_get_contents($url, false, stream_context_create($context));


	return '<Report><Email>info@mercadovial.com</Email><Acount>11471904</Acount><Pin>IF8LF9IPDD</Pin><StartDate>20100512</StartDate><EndDate>20100610</EndDate><XML>1</XML><State>1</State><Pays><Pay Trx_id="491"><Trx_Date>2010-05-31 05:30:36</Trx_Date><Trx_NombreComprador>Perea Diego</Trx_NombreComprador><Trx_Email>dpereavillegas@hotmail.com</Trx_Email><Trx_Payment>40.00</Trx_Payment><Trx_MontoNeto>36.25</Trx_MontoNeto><Trx_ShipAddress></Trx_ShipAddress><Trx_Comment></Trx_Comment><Trx_SellerDocumentType>DNI</Trx_SellerDocumentType><Trx_SellerDocumentNumber>25705507</Trx_SellerDocumentNumber><Trx_Number>10274718</Trx_Number><Trx_PaymentMethod>Codigo de Barras</Trx_PaymentMethod><Trx_PaymentMean>Pago Fácil</Trx_PaymentMean><Trx_Shares></Trx_Shares><Trx_BuyerDocumentType></Trx_BuyerDocumentType><Trx_BuyerDocumentNumber></Trx_BuyerDocumentNumber><Trx_BuyerPhone>0351-156-562750</Trx_BuyerPhone><Items><Item Item_Code="102"><Item_Quantity>1</Item_Quantity><Item_Description>1 Creditos de Venta: Basico</Item_Description><Item_Payment>40.00</Item_Payment></Item></Items></Pay><Pay Trx_id="492"><Trx_Date>2010-05-21 04:33:22</Trx_Date><Trx_NombreComprador>Perea Diego</Trx_NombreComprador><Trx_Email>dpereavillegas@hotmail.com</Trx_Email><Trx_Payment>40.00</Trx_Payment><Trx_MontoNeto>36.25</Trx_MontoNeto><Trx_ShipAddress></Trx_ShipAddress><Trx_Comment></Trx_Comment><Trx_SellerDocumentType>DNI</Trx_SellerDocumentType><Trx_SellerDocumentNumber>25705507</Trx_SellerDocumentNumber><Trx_Number>10204207</Trx_Number><Trx_PaymentMethod>Codigo de Barras</Trx_PaymentMethod><Trx_PaymentMean>Pago Fácil</Trx_PaymentMean><Trx_Shares></Trx_Shares><Trx_BuyerDocumentType></Trx_BuyerDocumentType><Trx_BuyerDocumentNumber></Trx_BuyerDocumentNumber><Trx_BuyerPhone>0351-156-562750</Trx_BuyerPhone><Items><Item Item_Code="102"><Item_Quantity>1</Item_Quantity><Item_Description>1 Creditos de Venta: Basico</Item_Description><Item_Payment>40.00</Item_Payment></Item></Items></Pay></Pays><Collections></Collections><Tickets></Tickets><Receptions></Receptions><Retreats></Retreats><Credits></Credits><Debits></Debits></Report>';

exit();
}

/*XML PARSE AUX**/
function startElement($parser, $name, $attrs)
{
    global $flagPays,$flagPay,$flagPayment,$orderAndMoney, $currentPayOrder;

    if($name=="PAYS"){
	$flagPays=true;
    }else if($flagPays && $name=="PAY"){
	$flagPay=true;
	$currentPayOrder=$attrs["TRX_ID"];	
    }else if($flagPay && $name=="TRX_PAYMENT"){
	$flagPayment = true;
    }
}

function endElement($parser, $name) 
{
    global $flagPays,$flagPay,$flagPayment,$orderAndMoney, $currentPayOrder;

    if($flagPay && $name=="TRX_PAYMENT"){
	$flagPayment = false;
    }
    if($name=="PAYS"){
	$flagPays=false;
    }if($flagPays && $name=="PAY"){
	$flagPay=false;	
    }
}

function characterData($parser, $data) 
{
   global $flagPays,$flagPay,$flagPayment,$orderAndMoney, $currentPayOrder;

   if($flagPay){
   	if($currentPayOrder!=0){
		$orderAndMoney[$currentPayOrder]=$data;
   	}
   }
}
/*XML PARSE AUX**/
/*
 * Los parametros deben ser strings con el siguiiente formato: yyyymmdd
 * Devuelve un array con numero de ordern y dinero de esa orden.
 * Si hay algun error agrega un campo "ERROR" en el array con dicho error
 * */
function getDineroMailData($startDate,$endDate){
	 
	global $flagPays,$flagPay,$flagPayment,$orderAndMoney, $currentPayOrder;
	
	$xml_parser = xml_parser_create();
	$myPostData = array(  'Email'=>DINEROMAIL_USER_MAIL,
			'Acount'=>DINEROMAIL_ACOUNT,
			'Pin'=>DINEROMAIL_PIN,
			'StartDate'=>$startDate,
			'EndDate'=>$endDate,
			'XML'=>'1');
	$xmlStream=doPost(DINEROMAIL_URL_CHECK,$myPostData);
	//PARSE XML
	xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
	xml_set_element_handler($xml_parser, "startElement", "endElement");
	xml_set_character_data_handler($xml_parser, "characterData");
	if (!xml_parse($xml_parser, $xmlStream)) {
	       $orderAndMoney["ERROR"]="Imposible conectar con el servidor de dinero mail o parsear el resultado de la consulta";
	}
	xml_parser_free($xml_parser);
	//END XML
	return $orderAndMoney;
}

?>
